<!-- 商品类目 -->
<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-button type="primary" @click="addForm" icon="el-icon-edit" size="mini">添加</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" border style="width: 100%" row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" lazy>
      <el-table-column prop="id" label="类目ID" min-width="12%" align="center">
      </el-table-column>
      <el-table-column prop="name" label="类目名" min-width="12%" align="center">
      </el-table-column>
      <el-table-column label="类目图标" min-width="12%" align="center">
        <template slot-scope="scope">
          <el-image :src="scope.row.iconUrl" alt="品牌商图片" :preview-src-list="srcList" style="width: 40px;height: 40px;">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="类目图片" min-width="12%" align="center">
        <template slot-scope="scope">
          <el-image :src="scope.row.picUrl" alt="品牌商图片" :preview-src-list="srcList" style="width: 40px;height: 40px;">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="keywords" label="关键字" show-overflow-tooltip min-width="12%" align="center">
      </el-table-column>
      <el-table-column prop="desc" label="简介" min-width="15%" align="center">
      </el-table-column>
      <el-table-column prop="level" label="级别" min-width="12%" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.level === 'L1' ? '' : 'info'">{{ getLevel(scope.row.level) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="13%" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" type="primary">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 模态框 -->
    <el-dialog title="创建" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="类目名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="关键字" :label-width="formLabelWidth">
          <el-input v-model="form.keywords" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="级别" placeholder="请选择" :label-width="formLabelWidth">
          <el-select v-model="form.level" @change="handleLevelChange">
            <el-option label="一级类目" value="L1"></el-option>
            <el-option label="二级类目" value="L2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="父类目" v-show="form.level == 'L2'" :label-width="formLabelWidth">
          <el-select v-model="form.pid" placeholder="请选择">
            <el-option v-for="item in tableData" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类目图标" :label-width="formLabelWidth">
          <el-upload class="avatar-uploader" :action="iconUrl" :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="类目图片" :label-width="formLabelWidth">
          <el-upload class="avatar-uploader" :action="iconUrl1" :show-file-list="false"
            :on-success="handleAvatarSuccess1">
            <img v-if="imageUrl1" :src="imageUrl1" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="类目简介" :label-width="formLabelWidth">
          <el-input v-model="form.desc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <!-- 新增 -->
        <el-button type="primary" @click="confirmFun" v-show="show">确 定</el-button>
        <!-- 编辑 -->
        <el-button type="primary" @click="editFun" v-show="!show">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, addList, editList, delList } from "@/api/scgl/category";
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      listLoading: true,
      // 表格数据
      tableData: [],
      queryParams: {}, //地址拼接的参数
      picUrl: '',
      srcList: [
        'http://admin.bsmall.byesame.com/wx/storage/fetch/g3ybo187ccun71o2tg03.jpg'
      ],
      // 查询条件
      formInline: {},
      id: '',
      name: '',
      // 模态框
      dialogFormVisible: false,
      form: {},
      formLabelWidth: '120px',
      rules: {
        name: [
          { required: true, message: '品牌商名称不能为空', trigger: 'blur' }
        ],
      },
      // 上传头像
      imageUrl: '',
      imageUrl1: '',
      iconUrl: "",//默认图片
      iconUrl1: "",//默认图片
      show: true,
      // 标签
      level: '',
      value: ''
    };
  },
  created() {
    this.fetchData();
    this.iconUrl = "/api/admin/storage/create";
    this.iconUrl1 = "/api/admin/storage/create";
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getList().then((response) => {
        console.log(response);
        this.tableData = response.data.list
        this.srcList = response.data.list.map(item => item.picUrl)
        this.listLoading = false;
      });
    },
    // 标签
    getLevel(level) {
      if (level == 'L1') {
        return '一级类目'
      } else if (level == 'L2') {
        return '二级类目'
      }
    },

    // 添加
    addForm() {
      this.dialogFormVisible = true
      this.form = {}
      this.show = true
      this.imageUrl = ''
      this.imageUrl1 = ''
    },
    // 上传图片
    handleAvatarSuccess(res, file) {
      console.log(res.data.url)
      this.form.iconUrl = res.data.url //给接口传递数据
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    handleAvatarSuccess1(res, file) {
      console.log(res.data.url)
      this.form.picUrl = res.data.url //给接口传递数据
      this.imageUrl1 = URL.createObjectURL(file.raw);
    },
    // 添加确认
    confirmFun() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addList(this.form).then((response) => {
            console.log(response);
            this.fetchData();
            this.dialogFormVisible = false
            this.$notify({
              title: "成功",
              message: "创建成功",
              type: "success",
            });
          }).catch((error) => {
            console.error("Error during submission:", error);
            this.$notify({
              title: "失败",
              message: "参数不对",
              type: "success",
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 编辑
    handleEdit(index, row) {
      console.log(index, row);
      this.show = false
      this.dialogFormVisible = true
      this.form = { ...row }
      this.imageUrl = row.iconUrl
      this.imageUrl1 = row.picUrl
    },
    // 监听标签的值
    handleLevelChange(value) {
      if (value === 'L1') {
        this.form.pid = 0;
        this.form.children = [];
      }
    },
    // 编辑确认
    editFun() {
      editList(this.form).then((response) => {
        console.log(response);
        this.fetchData();
        this.dialogFormVisible = false
        this.$notify({
          title: "成功",
          message: "更新成功",
          type: "success",
        });
      }).catch((error) => {
        console.error("Error during submission:", error);
        this.$notify({
          title: "失败",
          message: "参数不对",
          type: "success",
        });
      });
    },
    // 删除
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delList(row).then((response) => {
          console.log(response);
          this.fetchData();
          this.dialogFormVisible = false
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
          });
        }).catch((error) => {
          console.error("Error during submission:", error);
          this.$notify({
            title: "失败",
            message: "参数不对",
            type: "error",
          });
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
